package cmath.cmath;

import com.server.order.orderImpl;
import com.server.runtime.vehicleImpl;
import cmath.Convert.Convert;
import combat.protocol.data.DoublePoint;

public class Angle {

	private static DoublePoint _huidig;
	private static double _huidigx;
	private static double _huidigy;
	
	private static DoublePoint _target;
	private static double _targetx;
	private static double _targety;
	
	private static double _deltax;
	private static double _deltay;
	
	private static double _angle;
	
	
	public static double Calculate (vehicleImpl _object, orderImpl _order)
	{	
		_huidig = _object.getPosition();
		_huidigx = _huidig.getX();
		_huidigy = _huidig.getY();
		
		_target = Convert.ToDoublePoint(_order.getTarget());
		_targetx = _target.getX();
		_targety = _target.getY();
		
		
		_deltax = _targetx - _huidigx;
		_deltay = _targety - _huidigy;
		
		_angle = Math.toDegrees(Math.atan2(_deltay, _deltax)); 		//Links = +/-180 en rechtis = 0 (boven -90 en onder +90)
		_angle = To360Scale(_angle); 					//Geeft waarde terug tussen 0 - 360
		
		return _angle;
	}

	public static double CalculateAngleDiff(vehicleImpl _object, double _newangle)
	{
		//Berekend hoek rechtsom de eenheidscirkel
		
		double _huidigeangle = _object.getDirection();
		double _anglediff = (_newangle < _huidigeangle) ? (_huidigeangle - _newangle) : (_newangle - _huidigeangle);
		
		_anglediff = (int)_anglediff;
		return _anglediff;	//Returnwaarde tussen de 0 en de 360 
	}
	
	public static double CalculateTurnAngle(double _anglediff)
	{
		double _smallDif = _anglediff >= 180 ? 180 + _anglediff : _anglediff;
		
		double _turnangle = _smallDif;
		_turnangle = _turnangle >= 0.5 ? 0.5 : _turnangle; 
			
		_turnangle = Angle.To360Scale(_turnangle);
		return _turnangle;
	}
	
	public static double To360Scale(double _oldangle)
	{
		double _newangle;
		_newangle = _oldangle >= 360 ? (_oldangle - 360) : _oldangle;
		_newangle = _oldangle < 0 ? (_oldangle += 360) : _newangle;//_oldangle;
		
		return _newangle;
	}	
	
	
}
